/**
 * 
 */
package org.mspring.mlog.utils;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Calendar;

import javax.imageio.ImageIO;

import org.mspring.mlog.ServiceFactory;
import org.mspring.mlog.service.exception.StorageException;
import org.mspring.platform.utils.ImageUtils;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author Gao Youbo
 * @since 2013-6-3
 * @description
 * @TODO 头像处理相关操作
 */
public class AvatarUtils {

    public static final String AVATAR_FOLDER_NAME = "/avatar";
    public static final String AVATAR_NAME_PREFIX = "avatar-";
    public static final String AVATAR_IMAGE_TYPE = ImageUtils.ImageType.JPG;

    /**
     * 获取用户头像名称
     * 
     * @param userid
     * @return
     */
    public static String getAvatarFilename(Long userid) {
        return AVATAR_NAME_PREFIX + userid;
    }

    /**
     * 获取当前图像应该保存的文件夹位置
     * 
     * @return
     */
    public static String getAvatarFoldername() {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        return AVATAR_FOLDER_NAME + "/" + year + "/" + month + "/";
    }

    /**
     * 获取零时文件路径
     * 
     * @return
     */
    public static String getAvatarTempFilename(Long userid) {
        return getAvatarFilename(userid) + "_temp";
    }

    /**
     * 上传图像文件
     * 
     * @param mf
     * @param userid
     * @return 上传后的图片路径
     * @throws IOException
     * @throws StorageException
     */
    public static String uploadAvatar(MultipartFile mf, Long userid) throws StorageException, IOException {
        if (mf == null || userid == null) {
            return null;
        }
        String filename = getAvatarFoldername() + getAvatarTempFilename(userid) + "." + AVATAR_IMAGE_TYPE;
        filename = ServiceFactory.getStorageService().put(filename, mf.getInputStream());
        // File targetfile = ServiceFactory.getStorageService().get(filename);
        File targetfile = ServiceFactory.getStorageService().get(filename);
        try {
            BufferedImage target = ImageUtils.resize(mf.getInputStream(), 600, 500);
            ImageIO.write(target, ImageUtils.ImageType.JPG, targetfile);
            return ServiceFactory.getStorageService().getUrl(filename);
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 图片剪裁
     * 
     * @param file
     *            要剪切的图片
     * @param userid
     *            用户ID
     * @param startX
     * @param startY
     * @param endX
     * @param endY
     * @return 剪切后图片的路径
     */
    public static BufferedImage cropAvatar(File file, Long userid, int startX, int startY, int endX, int endY) {
        try {
            BufferedImage image = ImageUtils.crop(file, startX, startY, endX, endY);
            return image;
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 保存大头像
     * 
     * @param image
     * @param userid
     * @return
     * @throws IOException
     */
    public static String buildLargeAvatar(BufferedImage image, Long userid) throws IOException {
        // 文件保存的相对路径
        String relativeSavePath = getAvatarFoldername() + getAvatarFilename(userid) + "_large." + ImageUtils.ImageType.JPG;
        // 文件保存的绝对路径
        File absoluteFile = ServiceFactory.getStorageService().get(relativeSavePath);

        BufferedImage target = ImageUtils.resize(image, 300, 300);
        ImageIO.write(target, ImageUtils.ImageType.JPG, absoluteFile);

        return relativeSavePath;
    }

    /**
     * 保存小头像
     * 
     * @param image
     * @param userid
     * @return
     * @throws IOException
     */
    public static String buildSmallAvatar(BufferedImage image, Long userid) throws IOException {
        // 文件保存的相对路径
        String relativeSavePath = getAvatarFoldername() + getAvatarFilename(userid) + "_small." + ImageUtils.ImageType.JPG;
        // 文件保存的绝对路径
        File absoluteFile = ServiceFactory.getStorageService().get(relativeSavePath);

        BufferedImage target = ImageUtils.resize(image, 100, 100);
        ImageIO.write(target, ImageUtils.ImageType.JPG, absoluteFile);

        return relativeSavePath;
    }
}
